package com.life.notes.config.security.handler;

import cn.hutool.core.util.ObjectUtil;
import com.life.notes.config.utils.ResponseMsgUtil;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户未登录处理类
 * @author lishuqian
 * @date 2020/6/16
 */
@Component
public class UserAuthenticationEntryPointHandler implements AuthenticationEntryPoint {
    /**
     * 用户未登录返回结果
     * @author zwq
     * @date 2020/4/4
     * @param request
     * @param response
     * @param exception
     * @return
     **/
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception){
        // 未登录 或 token过期
        if (ObjectUtil.isNotNull(exception)){
            ResponseMsgUtil.responseJson(response,ResponseMsgUtil.error(401,exception.getMessage()));
        }else {
            ResponseMsgUtil.responseJson(response,ResponseMsgUtil.error(401,"jwtToken过期"));
        }
    }
}
